﻿using commonImportPlugin.Configs.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace commonImportPlugin.Configs.MedicalFile
{
    public class MedicalFileConfig :ConfigsBase, IcommonBase
    {
        //
        //    基本配置参数
        //
        #region
        /// <summary>
        ///工厂配置
        /// </summary>
        public string factory { set; get; }
        /// <summary>
        /// 筛选数据文件规则
        /// (文件名正则[#]文件内容正则)
        /// </summary>
        public string fileFilterRules { set; get; }
        /// <summary>
        /// 编码格式
        /// </summary>
        public string encoding { set; get; }

        /// <summary>
        /// 医保数据起始位置（1开始）
        /// </summary>
        public int dataIndex { set; get; }

        /// <summary>
        /// 医保数据分隔符
        /// </summary>
        public string[] dataSeparators { set; get; }
        /// <summary>
        /// 获取医保数据银行卡号数据规则
        /// </summary>
        public int[] subBankCardId { set; get; }
        /// <summary>
        /// 获取医保数据身份证号规则
        /// </summary>
        public int[] subPersonId { set; get; }
        /// <summary>
        /// 获取医保数据规则
        /// </summary>
        public int[] subMedicalData { set; get; }

        /// <summary>
        /// 添加在医保数据的左右分隔符
        /// </summary>
        public string[] addSeparators { set; get; }
        /// <summary>
        /// 检查医保数据格式的正则
        /// </summary>
        public string dataCheckRegex { set; get; }
        /// <summary>
        /// 该参数用于实现过滤之前已导入的数据
        /// 该参数值为：bankCardId,personId,ssCardId之一
        /// </summary>
        public string filterItem { set; get; }
        #endregion

        ///
        //    其他
        //
        #region
        private static MedicalFileConfig uniqueInstance;
        public void Init()
        {   
            MedicalFileConfig medicalFileConfig = new MedicalFileConfig();
            //检查参数配置的参数格式为：数字(+)&数字(-+)&数字(-+)
            string checkRegexs = @"^\d{1,}&(-\d{1,}|\d{1,})&(-\d{1,}|\d{1,})$";

            //工厂配置
            medicalFileConfig.factory = configs["MedicalFileFactory"];

            //文件扩展名
            if (!IsKeyExist("MedicalFileFilterRules"))
                throw new Exception(MedicalErrors.MedicalFileFilterRulesError);

            medicalFileConfig.fileFilterRules = configs["MedicalFileFilterRules"];

            //编码方式
            if (!IsKeyEmpty("MedicalFileEncoding"))
                medicalFileConfig.encoding = configs["MedicalFileEncoding"];
            else
                medicalFileConfig.encoding = "GB2312";//默认值

            //数据起始行（1开始)
            if (!IsKeyExist("MedicalFileDataIndex"))
                throw new Exception(MedicalErrors.MedicalFileDataIndexError1);

            if (!Regex.IsMatch(configs["MedicalFileDataIndex"], @"\d+"))
                throw new Exception(MedicalErrors.MedicalFileDataIndexError2);

            medicalFileConfig.dataIndex = int.Parse(configs["MedicalFileDataIndex"]);

            //医保数据分隔符

            if (!IsKeyExist("MedicalFileDataSeparators"))
                throw new Exception(MedicalErrors.MedicalFileSeparatorsError);

            medicalFileConfig.dataSeparators = ToStringArrays(configs["MedicalFileDataSeparators"]);

            //截取银行卡号
            if (!IsKeyEmpty("MedicalFileSubBankCardId"))
            {
                if (!Regex.IsMatch(configs["MedicalFileSubBankCardId"], checkRegexs))
                    throw new Exception(MedicalErrors.MedicalFileSubBankCardIdError);

                medicalFileConfig.subBankCardId = ToIntArrays(configs["MedicalFileSubBankCardId"]);
            }

            //截取身份证号
            if (!IsKeyEmpty("MedicalFileSubPersonId"))
            {
                if (!Regex.IsMatch(configs["MedicalFileSubPersonId"], checkRegexs))
                    throw new Exception(MedicalErrors.MedicalFileSubPersonIdError);

                medicalFileConfig.subPersonId = ToIntArrays(configs["MedicalFileSubPersonId"]);
            }

            //截取医保数据
            if (!IsKeyEmpty("MedicalFileSubMedicalData"))
            {
                if (!Regex.IsMatch(configs["MedicalFileSubMedicalData"], checkRegexs))
                    throw new Exception(MedicalErrors.MedicalFileSubMedicalDataError);

                medicalFileConfig.subMedicalData = ToIntArrays(configs["MedicalFileSubMedicalData"]);
            }

            //添加在医保数据左右的符号
            if (IsKeyExist("MedicalFileAddSeparators"))
            {
                medicalFileConfig.addSeparators = new string[2];
                string[] addSeperates=ToStringArrays(configs["MedicalFileAddSeparators"]);

                if(addSeperates.Length>2)//参数个数错误
                     throw new Exception(MedicalErrors.MedicalFileAddSeparatorsError);

                //补‘<’ 由于xml<R><BNJK><R>中不能获取<BNJK>
                for (int i = 0; i < addSeperates.Length; i++)
			    {
			        if (Regex.IsMatch(addSeperates[i],@".*\>$"))
                        addSeperates[i]="<"+addSeperates[i];
			    }

                if (addSeperates.Length == 1)
                    medicalFileConfig.addSeparators[0] = addSeperates[0];
                else               
                    medicalFileConfig.addSeparators = addSeperates;
            }

            //医保数据格式检查正则
            if (IsKeyExist("MedicalFileDataCheckRegex"))
            {
                medicalFileConfig.dataCheckRegex = configs["MedicalFileDataCheckRegex"];
            }

            //过滤数据项（1开始
            if (!IsKeyEmpty("MedicalFileFilterItem"))
            {
                string[] items = new string[] { "bankCardId", "personId", "ssCardId" };
                string item = configs["MedicalFileFilterItem"].Trim();

                if (!items.Contains(item))
                    throw new Exception(MedicalErrors.MedicalFileFilterItemError);

                medicalFileConfig.filterItem = configs["MedicalFileFilterItem"];
            }

            uniqueInstance = medicalFileConfig;
        }
        /// <summary>
        /// 获取配置
        /// </summary>
        /// <returns></returns>
        public static MedicalFileConfig GetConfig()
        {
            if (uniqueInstance == null)
            {
                return null;
            }
            return uniqueInstance;
        }
        #endregion
    }
}
